package com.css.bjdt_core.validation.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.service.CellDataService;
import com.css.bjdt_core.cell.service.CellService;
import com.css.bjdt_core.executive.report.ReportRequest;
import com.css.bjdt_core.report.entity.RptTemplate;
import com.css.bjdt_core.report.entity.RptTemplateUnitRela;
import com.css.bjdt_core.report.service.ReportTemplateCommentService;
import com.css.bjdt_core.report.service.ReportTemplateManageService;
import com.css.bjdt_core.report.service.ReportTemplateUnitRelaService;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.validation.entity.ValidationInfo;

@Service
public class ReportCommentValidateService {
	@Autowired
	private ReportTemplateUnitRelaService reportTemplateUnitRelaService;
	@Autowired
	private CellService cellService;
	@Autowired
	private ValidationInfoService validationInfoService;
	@Autowired
	private ReportTemplateCommentService reportTemplateCommentService;
	@Autowired
	private ReportTemplateManageService reportTemplateManageService;
	@Autowired
	private CellDataService cellDataService;

	//----------------------------------------高杨（报表批注校核用的）--------------------------------------------------------
	/**
	 * 报表批注校核
	 * @param annualId
	 * @param templateId
	 * @param unitIds
	 */
	@Deprecated
	public void reportCommentValidation(String annualId, String templateId, String batchId, String unitType,
			String... unitIds) {
		for (String unitId : unitIds) {
			if (templateId == null) {
				insertReportCommentValidationAll(annualId, unitId, batchId);
			} else {
				insertReportCommentValidation(annualId, templateId, unitId, batchId, unitType);
			}

		}
	}

	//像批注校核里面插入数据(单张表的生成批注校核信息)
	public List<ValidationInfo> insertReportCommentValidation(String annualId, String templateId, String dzzId,
			String batchId, String unitType) {
		RptTemplate rptTemplate = reportTemplateManageService.getAnRptTemplate(templateId);
		//首先查询出来报表模板中那些单元格是需要批注的
		List<Cell> cellList = cellService.getAllNeedCommentCell(templateId);
		List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
		Map<String, Integer> commentCounts = null;
		Map<String, String> cellValues = null;
		if(!cellList.isEmpty()) {
			commentCounts = reportTemplateCommentService.getReportCellCommentCount(templateId, annualId, dzzId);
			cellValues = cellDataService.getCellValuesForColRow( annualId,  templateId, dzzId,  unitType);
		}
		for (Cell cell : cellList) {
			//查批注表里是否有记录
			String key = cell.getAbsRowNum() + "_" + cell.getAbsColNum();
			String cellValue = cellValues.get(key);
			Integer commentCount = commentCounts.get(key);
			//判断cellValue是否为0，为0的情况下补做报表批注校核
			if (cellValue == null || cellValue.equals("0")) {
				continue;
			} else {
				if (commentCount == null || commentCount == 0) {
					ValidationInfo validationInfo = new ValidationInfo();
					validationInfo.setId(CommonUtil.getUUID());
					validationInfo.setValidationType("5");
					validationInfo.setValidationState("2");
					validationInfo.setCellId(cell.getCellId());
					if (null == cell.getRelColNum() || ("").equals(cell.getRelColNum())) {
						validationInfo.setActualExp(rptTemplate.getName() + "的补充资料了需要填写批注");
					} else {
						validationInfo.setActualExp(rptTemplate.getName() + "的"
								+ CommonUtil.numToLetter(cell.getRelColNum() + "") + cell.getRelRowNum() + "需要填写批注");
					}
					validationInfo.setDzzId(dzzId);
					validationInfo.setBatchId(batchId);
					validationInfo.setTemplateId(templateId);
					validationInfoList.add(validationInfo);
				}
			}
		}
		return validationInfoList;
		//validationInfoService.batchInsertValidationInfo(annualId, validationInfoList);
	}

	//像批注校核里面插入数据(生成该单位的整个批注校核)
	public void insertReportCommentValidationAll(String annualId, String dzzId, String batchId) {
		//先根据单位ID和报表版本ID查询出所有的该单位下的需要填写的报表模板id
		ReportRequest reportRequest = new ReportRequest();
		List<RptTemplateUnitRela> viewRptTemplateUnitList = reportTemplateUnitRelaService
				.getTemplateUnitRela(reportRequest);
		List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
		for (RptTemplateUnitRela viewRptTemplateUnit : viewRptTemplateUnitList) {
			//首先查询出来报表模板中那些单元格是需要批注的
			List<Cell> cellList = cellService.getAllNeedCommentCell(viewRptTemplateUnit.getTemplateId());
			for (Cell cell : cellList) {
				//查批注表里是否有记录
				String commentCount = reportTemplateCommentService.getReportCellCommentCount(
						viewRptTemplateUnit.getId(), cell.getAbsColNum(), cell.getAbsRowNum(), annualId, dzzId);
				if (commentCount.equals("0")) {
					ValidationInfo validationInfo = new ValidationInfo();
					validationInfo.setId(CommonUtil.getUUID());
					validationInfo.setValidationType("5");
					validationInfo.setValidationState("2");
					validationInfo.setCellId(cell.getCellId());
					validationInfo.setActualExp(
							CommonUtil.numToLetter(cell.getRelColNum() + "") + cell.getRelRowNum() + "需要填写批注");
					validationInfo.setDzzId(dzzId);
					validationInfo.setBatchId(batchId);
					validationInfo.setTemplateId(viewRptTemplateUnit.getTemplateId());
					validationInfoList.add(validationInfo);
				}
			}
		}
		validationInfoService.batchInsertValidationInfo(annualId, validationInfoList);
	}

	//----------------------------------------完--------------------------------------------------------

}
